/**
 * Created by xc on 2017/11/21.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var async  = require( 'async'); // 同步控制

//--------------------------------------------------------------------------------------
// Schema 结构
var YjxTradeOdrDtlWmModel = function(obj){
    YjxBaseModel.call(this,obj);

  /*  CREATE TABLE `yjx_trade_odr_dtl_wm` (
  `detail_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单明细id',
  `detail_no` varchar(50) NOT NULL DEFAULT '' COMMENT 'itb自定义子订单号',
  `order_id` bigint(20) NOT NULL COMMENT '订单id',
  `unit` varchar(20) NOT NULL DEFAULT '次' COMMENT '计量单位',
  `qty` bigint(20) NOT NULL DEFAULT '1' COMMENT '数量',
  `uni_doc_id` bigint(20) NOT NULL COMMENT '用户作品id',
  `fee` bigint(12) NOT NULL DEFAULT '0' COMMENT '订单明细金额（单位为rmb分）',
  `state` int(3) NOT NULL DEFAULT '0' COMMENT '去水印(子)订单状态:',
  `desc` varchar(200) DEFAULT '' COMMENT '订单描述',
  `create_time` datetime NOT NULL,
  `update_time` datetime NOT NULL,
  `update_by` varchar(50) NOT NULL,
  `del_flg` int(1) NOT NULL DEFAULT '0',
        PRIMARY KEY (`detail_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='子订单表:去水印订单'
*/
};

YjxTradeOdrDtlWmModel.prototype = new YjxBaseModel();

// 取得表名
YjxTradeOdrDtlWmModel.prototype.getTableName = function(){
    return 'yjx_trade_odr_dtl_wm';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxTradeOdrDtlWmModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'detail_id='+this.detail_id;
};


/**
 * 根据订单 id 获取该作品支付状态
 *
 * @author 王俊
 * @date 2018-05-22
 *
 * @param orderId      订单标识
 * @param callback     回调
 *          err:  查询失败
 *          data: [yjx_uni_user_doc 行数据对象]
 */
YjxTradeOdrDtlWmModel.prototype.getOrderUniDocPayState = function(orderId, callback) {
    

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('YjxTradeOdrDtlVipModel.updVipOdrExpTime Err:'+err);
            return callback(err,null);
        }

        var sql =
            ' SELECT doc.* ' +
            ' FROM yjx.yjx_trade_odr_dtl_wm wm, ' +
            '      yjx.yjx_uni_user_doc doc ' +
            ' WHERE wm.order_id = ' + orderId +
            '   AND doc.uni_doc_id = wm.uni_doc_id ' +
            '   AND wm.del_flg=0 ' +
            '   AND doc.del_flg=0 ';
        console.log(sql);
        connection.query(sql, function(error, results) {
            // And done with the connection.
            connection.release();
            return callback(error, results);
        });
    });
};



//--------------------------------------------------------------------------------------
// 导出
module.exports = YjxTradeOdrDtlWmModel;